# -*- coding:utf-8 -*-

## =====================================================================
## Copyright (C) 2007-2011  Jean-Philippe GOLAY
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
## =====================================================================


from Tkinter import *
import sys, os
import time
from iVariable import Glob

class TimePopupGUI(Menu):
	def __init__(self, parent=None, tearoff=0, event=None, field=None, varHour=None, path=None, log=None):
		Menu.__init__(self, parent)
		self.parent=parent
		self.event=event
		self.field=field
		self.varHour=varHour
		self.hoursListCB = Glob.HOURS_LIST
		self.minutesListCB = Glob.MINUTES_LIST
		self.path=path
		self.log=log
		# Afficher un icon personnalisé
		try:
			if os.name =="posix":
				img = PhotoImage(file=self.path+"/icons/Time-icon.gif")
				self.tk.call("wm", "iconphoto", self._w, img)
			else:
				self.iconbitmap(default=self.path+"/icons/Time-icon.gif")
		except TclError:
			self.log.warning("Le window manager ne supporte pas l'icône que je veux lui mettre...")
		self.log.info("\tTimePopupGUI\tStart")
		self.makePopup()

	def makePopup(self):
		self.add_command(label=u"Maintenant", command=self.popupNow)
		self.add_command(label=u"08h00", command=lambda x="08":self.popupAction(x))
		self.add_command(label=u"10h00", command=lambda x="10":self.popupAction(x))
		self.add_command(label=u"12h00", command=lambda x="12":self.popupAction(x))
		self.add_command(label=u"14h00", command=lambda x="14":self.popupAction(x))
		self.add_command(label=u"16h00", command=lambda x="16":self.popupAction(x))
		self.add_command(label=u"18h00", command=lambda x="18":self.popupAction(x))
		self.add_command(label=u"20h00", command=lambda x="20":self.popupAction(x))
		self.add_command(label=u"22h00", command=lambda x="22":self.popupAction(x))
		self.add_command(label=u"00h00", command=lambda x="00":self.popupAction(x))
		# display the popup menu
		try:
			self.tk_popup(self.event.x_root, self.event.y_root, 0)
		finally:
			# make sure to release the grab (Tk 8.0a1 only)
			self.grab_release()

	def popupAction(self,hour):
		self.varHour[0].set(hour)
		self.varHour[1].set("00")
		self.varHour[2].selectitem(int(hour))
		self.varHour[3].selectitem(0)

	def popupNow(self):
		hour = time.strftime("%H" ,time.localtime())
		minute = time.strftime("%M" ,time.localtime())
		# Affichage de l'heure dans la ComboBox 
		count = 0
		for h in self.hoursListCB:
			if h == hour: self.varHour[2].selectitem(count)
			count+=1
		# Affichage des minutes dans la ComboBox
		count = 0
		for m in self.minutesListCB:
			if m == minute: self.varHour[3].selectitem(count)
			count+=1
		# Mise à jour des variables heure et minutes
		self.varHour[0].set(hour)
		self.varHour[1].set( minute)